package com.wenyl.juc.thread;

/**
 * 线程优先级范围是1-10，默认优先级是5，10最高
 * 线程优先级无法保证线程执行顺序，优先级高的线程获取CPU资源概率大，低优先级的并非没有执行机会
 * 主线程main的优先级是5
 *
 * @author Swimming Dragon
 * @description: TODO
 * @date 2024年11月05日 22:30
 */
public class ThreadPriority {
    public void priority(){
        UserThread t1 = new UserThread();
        t1.setName("线程1");
        t1.setPriority(10);

        Thread t2 = new Thread(new UserRun(), "线程2");
        t2.setPriority(1);

        t1.start();
        t2.start();
    }
}
class UserThread extends Thread{
    @Override
    public void run() {
        for(int i=0;i<10;i++){
            System.out.println(Thread.currentThread().getName()+",第"+i+"次执行！");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
class UserRun implements Runnable{

    @Override
    public void run() {
        for(int i=0;i<10;i++){
            System.out.println(Thread.currentThread().getName()+",第"+i+"次执行！");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}